import { Web } from "@/api/configApi";
import { ref } from "vue";

const web = new Web();

const imageUrl = ref("");
const avatarUrl = ref("");
const fileUrl = ref("");
const fileDownloadUrl = ref("");
const appResource = ref("");
const filePreviewUrl = ref("");

export default () => {
  const getWebConfig = () => {
    let webConfig = sessionStorage.getItem("webConfig");

    //如果本地存在数据，那么就从本地拿取数据
    if (!!webConfig) {
      let data = JSON.parse(webConfig);
      imageUrl.value = `${data.imageUrl}?fileId=`;
      fileUrl.value = `${data.fileUrl}?fileId=`;
      avatarUrl.value = `${data.avatarUrl}?fileId=`;
      filePreviewUrl.value = `${data.userProtocolBaseUrl}/`;
      fileDownloadUrl.value = `${data.docUrl}?fileId=`;
      appResource.value = `${data.appResourceUrl}?fileId=`;
    } else {
      //如果本地不存在那么就发送请求拿取配置
      web.fetch().then((res) => {
        imageUrl.value = `${res.data.imageUrl}?fileId=`;
        fileUrl.value = `${res.data.fileUrl}?fileId=`;
        avatarUrl.value = `${res.data.avatarUrl}?fileId=`;
        filePreviewUrl.value = `${res.data.userProtocolBaseUrl}/`;
        fileDownloadUrl.value = `${res.data.docUrl}?fileId=`;
        appResource.value = `${res.data.appResourceUrl}?fileId=`;
        sessionStorage.setItem("webConfig", JSON.stringify(res.data));
      });
    }
  };

  return {
    imageUrl,
    avatarUrl,
    fileUrl,
    fileDownloadUrl,
    appResource,
    filePreviewUrl,
    getWebConfig,
  };
};
